package utils.ai
{
	import global.GameType;
	
	import model.card.ICardData;
	
	import utils.rule.IRuleResult;
	import utils.rule.RuleResult;

	public class AutoHintPlayCard
	{
		public static function autoHint3PlayCard(lastCardDatas:Array,holdCardDatas:Array):IRuleResult
		{
			var ruleResult:IRuleResult = new RuleResult();
			if (lastCardDatas.length < 3) return ruleResult;
			holdCardDatas = CardTools.cloneCardDatas(holdCardDatas);
			
			var results:Array = [];
			var jokers:Array = CardTools.getJokers(holdCardDatas);
			var rest:Array = CardTools.getRest(holdCardDatas);
			if (CardTools.isCardDatasStraight(lastCardDatas))//如果上次出牌是连牌
			{	
				results = results.concat(getTheStraight(lastCardDatas,holdCardDatas));
				if (jokers.length > 0)
				{
					var aroundCardDatas:Array = getTheStraightAroundCards(lastCardDatas,holdCardDatas);
					results = results.concat(combineJokerToAroundStraight(lastCardDatas,jokers[jokers.length - 1],aroundCardDatas));
				}
				results = results.concat(CardTools.getAllBombs(holdCardDatas));
				if (jokers.length > 0)
				{
					results = results.concat(CardTools.combineJokerToBombs(jokers[jokers.length - 1],rest));
				}
			}		
			else if (CardTools.isCardDatasTheSame(lastCardDatas))//如果上次出牌是炸弹
			{
				var bombs:Array = CardTools.getAllBombs(holdCardDatas);
				var bomb:Array ;
				for (var i:int=bombs.length - 1; i>=0; i--)
				{
					bomb = bombs[i];
					if (CardTools.compairTwoBombs(bomb,lastCardDatas))
					{
						results.push(bomb);
					}
				}
			}
			else if (CardTools.isPairStraight(lastCardDatas))//如果上次出牌是板凳对
			{
				
				var generateBigPair:Array = CardTools.generateBigPairStraight(lastCardDatas);
				var checkPairStarightGaps:Array = CardTools.checkBigPairStaright(generateBigPair,holdCardDatas);
				if (checkPairStarightGaps && checkPairStarightGaps.length == 0)
				{
					results.push(CardTools.setGenerateBigPairStaright(generateBigPair,holdCardDatas));
				}
				
				if (jokers.length > 0)
				{
					var result:Array;
					if (checkPairStarightGaps && checkPairStarightGaps.length == 1)
					{
						var joker:ICardData = jokers[0];
						joker = joker.clone();
						joker.jokerValue = checkPairStarightGaps[0].value;
						result = CardTools.setGenerateBigPairStaright(generateBigPair,holdCardDatas);
						result.push(joker);
						CardTools.sortCards(result);
						results.push(result);
					}
					else if (checkPairStarightGaps && checkPairStarightGaps.length == 0)
					{
						//取掉板凳对中任意一个
						result = CardTools.setGenerateBigPairStaright(generateBigPair,holdCardDatas);
						results = results.concat(combineJokerToPairStaright(result,jokers[0]));
						
					}	
				}
				//放炸弹
			
				results = results.concat(CardTools.getAllBombs(holdCardDatas));
				if (jokers.length > 0)
				{
					results = results.concat(CardTools.combineJokerToBombs(jokers[jokers.length - 1],rest));
				}
				
			}
			
			if (jokers.length == 2)
			{
				results.push(jokers);
			}
			ruleResult.results = results;
			
			if (results.length > 0)
			{
				ruleResult.correct = true;
			}
			
			return ruleResult;
		}
		
		//自动提示对应2张的情况
		public static function autoHint2PlayCard(lastCardDatas:Array,holdCardDatas:Array):IRuleResult
		{
			var ruleResult:IRuleResult = new RuleResult();
			if (lastCardDatas.length != 2) return ruleResult;
			holdCardDatas = CardTools.cloneCardDatas(holdCardDatas);
			
			var jokers:Array = CardTools.getJokers(holdCardDatas);
			var rest:Array = CardTools.getRest(holdCardDatas);
			
			var pairs:Array = CardTools.getPairs(rest);
			var pair:Array;
			var results:Array = [];
			for (var i:int=pairs.length-1;i>=0;i--)
			{
				pair = pairs[i];
				if (pair[0].value > lastCardDatas[0].value)
				{	
					if (pair[0].value == GameType.CARD_VALUE_2)
					{
						results.push(pair);
					}
					else if (pair[0].value == lastCardDatas[0].value + 1)
					{
						results.push(pair);
					}
				}	
			}
			
			holdCardDatas = CardTools.cloneCardDatas(holdCardDatas);
			
			if (jokers.length > 0)
			{
				var valueCardDatas:Array = CardTools.getValueCard(lastCardDatas[0].value + 1,holdCardDatas);
				if (valueCardDatas.length > 0)
				{
					var combineJoker:ICardData = jokers[jokers.length - 1];
					combineJoker = combineJoker.clone();
					combineJoker.jokerValue = valueCardDatas[0].value;
					results.push([valueCardDatas[0].clone(),combineJoker]);
				}
			}
			
			var bombs:Array = CardTools.getAllBombs(holdCardDatas);
			results = results.concat(bombs);
			
			if (jokers.length > 0)
			{
				
				results = results.concat(CardTools.combineJokerToBombs(jokers[jokers.length - 1],rest));
			}
			
			ruleResult.results = results;
			
			if (jokers.length == 2)
			{
				results.push(jokers);
			}
			
			if (results.length > 0)
			{
				ruleResult.correct = true;
			}
			
			return ruleResult;
		}
		
		//自动提示对应一张牌的情况
		public static function autoHint1PlayCard(lastCardDatas:Array,holdCardDatas:Array):IRuleResult
		{
			var ruleResult:IRuleResult = new RuleResult();
			if (lastCardDatas.length != 1) return ruleResult;
			
			holdCardDatas = CardTools.cloneCardDatas(holdCardDatas);
			
			var results:Array = [];
			
			var lastCard:ICardData = lastCardDatas[0];
			var holdCard:ICardData;
			for (var i:int=holdCardDatas.length - 1; i>=0; i--)
			{
				holdCard = holdCardDatas[i];
				if (holdCard.value == GameType.CARD_VALUE_JOKER) continue;
				if (holdCard.value > lastCard.value)
				{
					if (holdCard.value == lastCard.value + 1 || holdCard.value == GameType.CARD_VALUE_2)
					{
						ruleResult.correct = true;
						CardTools.pushHintResult([holdCard.clone()],results);
					}
				}
			}
			
			var bombs:Array = CardTools.getAllBombs(holdCardDatas);
			results = results.concat(bombs);
			
			
			var jokers:Array = CardTools.getJokers(holdCardDatas);
			var rest:Array = CardTools.getRest(holdCardDatas);
			if (jokers.length > 0)
			{
				holdCardDatas = CardTools.cloneCardDatas(holdCardDatas);
				results = results.concat(CardTools.combineJokerToBombs(jokers[0],rest));
			}
			ruleResult.results = results;
			
			if (jokers.length == 2)
			{
				results.push(jokers);
			}
			
			if (results.length > 0)
			{
				ruleResult.correct = true;
			}
			
			return ruleResult;
		}
//==============================================处理板凳对的方法
		private static function combineJokerToPairStaright(pairStraights:Array,joker:ICardData):Array
		{
			var results:Array = [];
			var pairCardData:ICardData;
			var array:Array;
			for (var i:int=0; i<pairStraights.length; i+=2)
			{
				pairCardData = pairStraights[i];
				array = CardTools.cloneCardDatas(pairStraights);
				array.splice(i,1);
				joker = joker.clone();
				joker.jokerValue = pairCardData.value;
				array.push(joker);
				CardTools.sortCards(array);
				results.push(array);
			}
			return results;
		}

//================================================处理连牌的方法

		private static function combineJokerToAroundStraight(lastCardDatas:Array,joker:ICardData,aroundCardDatas:Array):Array
		{
			joker = joker.clone();
			aroundCardDatas = CardTools.cloneCardDatas(aroundCardDatas);
			var bigAroundCardData:ICardData = aroundCardDatas[0];
			var smallAroundCardData:ICardData = aroundCardDatas[aroundCardDatas.length - 1];
			var results:Array = [];
			
			if (aroundCardDatas.length == 0)
			{
				return results;
			}
			
			if (aroundCardDatas.length == lastCardDatas.length)
			{
				for (var m:int=0;m<aroundCardDatas.length;m++)
				{
					joker = joker.clone();
					joker.jokerValue = aroundCardDatas[m].value;
					var array:Array = CardTools.cloneCardDatas(aroundCardDatas);
					array.splice(m,1);
					results.push(array.concat(joker));
				}
			
			}
			else
			{
				if (CardTools.isCardDatasStraight(aroundCardDatas))
				{
					if (bigAroundCardData.value < GameType.CARD_VALUE_A)
					{
						joker = joker.clone();
						joker.jokerValue = bigAroundCardData.value + 1;
						results.push(aroundCardDatas.concat(joker));	
					}
					
					if (smallAroundCardData.value > GameType.CARD_VALUE_3)
					{
						joker = joker.clone();
						joker.jokerValue = smallAroundCardData.value - 1;
						results.push(aroundCardDatas.concat(joker));
					}
				}
				else
				{
					var lastAroundCardData:ICardData;
					var currAroundCardData:ICardData;
					for (var i:int=0; i<aroundCardDatas.length; i++)
					{
						if (lastAroundCardData == null)
						{
							lastAroundCardData = aroundCardDatas[i];
						}
						else
						{
							currAroundCardData = aroundCardDatas[i];
							if (currAroundCardData.value != lastAroundCardData.value - 1)
							{
								joker = joker.clone();
								joker.jokerValue = lastAroundCardData.value - 1;
								results.push(aroundCardDatas.concat(joker));
							}
						}
					}
				}
			}
			
			//var results:Array = CardTools.combineJokerWithRestToStraight(joker,aroundCardDatas);
			
			results = filterStraightFromLastCardDatas(lastCardDatas,results);
			return results;
		}

		
		//根据出的连牌过滤掉连牌集合
		private static function filterStraightFromLastCardDatas(lastCardDatas:Array,arrays:Array):Array
		{
			var results:Array = [];
			var cardDatas:Array;
			var lastBigCardData:ICardData = lastCardDatas[0];
			var resultBigCardData:ICardData;
			for (var i:String in arrays)
			{
				cardDatas = arrays[i];
				CardTools.sortCards(cardDatas);
				if (CardTools.isCardDatasStraight(cardDatas))
				{
					resultBigCardData = cardDatas[0];
					if (lastBigCardData.value + 1 == resultBigCardData.value && cardDatas.length == lastCardDatas.length)
					{
						results.push(cardDatas);
					}
				}
			}
			return results;
		}
		
		//根据上次出的连牌 取得手上牌相对周边的牌
		private static function getTheStraightAroundCards(lastCardDatas:Array,holdCardDatas:Array):Array
		{
			var results:Array = [];
			if ( !CardTools.isCardDatasStraight(lastCardDatas))
			{
				return results;
			}
			lastCardDatas = CardTools.cloneCardDatas(lastCardDatas);
			holdCardDatas = CardTools.cloneCardDatas(holdCardDatas);
			CardTools.sortCards(lastCardDatas);
			var rest:Array = CardTools.getRest(holdCardDatas);
			var cardData:ICardData;
			var lastCardData:ICardData;
			for (var i:int=0; i<rest.length; i++)
			{
				cardData = rest[i];
				for (var n:int=0; n<lastCardDatas.length; n++)
				{
					lastCardData = lastCardDatas[n];
					if (lastCardData.value + 1 == cardData.value)
					{
						lastCardDatas.splice(n,1);
						results.push(cardData.clone());
						break;
					}
				}
			}
			
			return results;
		}
		
		//根据出的连牌 返回连牌
		private static function getTheStraight(lastCardDatas:Array,holdCardDatas:Array):Array
		{
			var results:Array = [];
			
			if (!CardTools.isCardDatasStraight(lastCardDatas))
			{
				return results;
			}
			
			var length:int = 0;
			holdCardDatas = CardTools.cloneCardDatas(holdCardDatas);
			var bigLastCardData:ICardData = lastCardDatas[0];
			var lastCardData:ICardData;
			var currentCardData:ICardData;
			var straightLastCardData:ICardData;
			var rest:Array = CardTools.getRest(holdCardDatas);
			for (var i:int=0; i<rest.length; i++)
			{
				if (lastCardData == null)
				{
					lastCardData = rest[i];
				}
				else
				{
					currentCardData = rest[i];
					if (straightLastCardData && straightLastCardData.value - length == currentCardData.value
						&& length < lastCardDatas.length)
					{
						results.push(currentCardData);
						length++;
					}
				}
				
				if ( rest[i].value == bigLastCardData.value + 1 && rest[i].value != GameType.CARD_VALUE_2)
				{
					if (straightLastCardData == null)
					{
						length++;
						straightLastCardData = rest[i];
						results.push(straightLastCardData);
					}
				}
			}
			
			if (results.length != lastCardDatas.length)
			{
				results.length = 0;
			}
			else
			{
				results = [results];
			}
			return results;
		}
	}
}